<template>
	<uni-nav-bar title="车上生活" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package_sale" style="padding:10rpx;">
		<view class="package"
			style="border-radius:30px;text-align:center;color:#666;margin:0px;background: #ECF1F5;width:100%;padding:15rpx 0px;"
			@click="navTo('gemstone/shop')">搜索商品</view>
	</view>
	<WelfareShop @carDeatil="carDeatil" ref="welfare"></WelfareShop>
	<!-- 底部购物车 -->
	<CarBottom @init="init()" @carDeatil="carDeatil" ref="bottom"></CarBottom>
</template>

<script setup lang="ts">
	import { navBack, navTo } from '@/utils/navigator';
	import { onShow } from '@dcloudio/uni-app';
	import CarBottom from '@c/reservation/carBottom';
	import WelfareShop from '@c/order/welfareShop';
	import { showLoading, errorToast } from '@/utils/prompt';
	import { ref } from 'vue';
	import { toPublish } from '@mqtt';
	import { getDriverID } from '@/stores/driverID';
	const driver_id = getDriverID();
	const bottom = ref(null)
	const welfare = ref(null)
	function init() {
		welfare.value.change()
	}
	// 购物车调整
	function carDeatil(id : number, num : number, type : string, fathID : number) {
		showLoading()
		const payload = {
			driver_id,
			goods_id: id,
			type: type
		};
		const topic = num === 1 ? 'score/jianCart' : 'score/addCart'
		toPublish(topic,
			payload,
			(obj : any) => {
				uni.hideLoading();
				const { code, msg } = obj;
				if (code === 1) {
					console.log(fathID)
					if (!fathID) {
						shopList(type, num, id)
					} else {
						shopList(type, num, fathID)
					}
					bottom.value.carLisst()
				} else {
					errorToast(msg)
				}
			}
		);
	}
	function shopList(type : string, num : number, id : number) {
		switch (type) {
			case 'gem':
				welfare.value.orderLists.map((item) => {
					if (item.id == id && num == 1) {
						item.cart_gem_nums--
					} else if (item.id == id && num == 2) {
						item.cart_gem_nums++
					}
				})
				break;
			case 'money':
				welfare.value.orderLists.map((item) => {
					if (item.id == id && num == 1) {
						item.cart_money_nums--
					} else if (item.id == id && num == 2) {
						item.cart_money_nums++
					}
				})
				break;
		}

	}
</script>

<style scoped lang="less">
	page {
		display: flex;
		flex-direction: column;
		height: 100%;
	}
</style>